package cn.edu.sdyu.ie.crs.api;

import cn.edu.sdyu.ie.crs.common.response.PageVO;
import cn.edu.sdyu.ie.crs.common.response.Result;
import cn.edu.sdyu.ie.crs.domain.Classroom;
import cn.edu.sdyu.ie.crs.domain.ClassroomPlanning;
import cn.edu.sdyu.ie.crs.service.ClassroomPlanningService;
import cn.edu.sdyu.ie.crs.service.ClassroomService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.Parameter;
//import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "教室计划管理", description = "教室计划信息管理接口")
@Slf4j
@RestController
@RequestMapping("api/classroomPlanning")
public class ClassroomPlanningController {

    @Autowired
    private ClassroomPlanningService classroomPlanningService;

    /**
     * 添加新教室计划信息
     *
     * @param classroomPlanning
     * @return
     */
    @Operation(tags = "教室计划管理", summary = "添加教室计划信息")
    @PostMapping("")
    public Result<Void> addClassroomPlanning(ClassroomPlanning classroomPlanning) {

        if (classroomPlanning.getId() != null && !classroomPlanning.getId().equals(0)) {
            log.warn("请求ID参数，若更新对象，请使用PUT方式发起请求。");
            return Result.failure();
        }
        classroomPlanningService.saveClassroomPlanning(classroomPlanning);
        return Result.success();
    }

    /**
     * 更新教室信息
     *
     * @param classroomPlanning
     * @return
     */
    @Operation(tags = "教室计划管理", summary = "修改教室计划信息")
    @PutMapping("")
    public Result<Object> updateClassroomPlanning(ClassroomPlanning classroomPlanning) {

        if (classroomPlanning.getId() == null || classroomPlanning.getId().equals(0)) {
            log.warn("未请求ID参数，使用POST方式创建对象。当前不会更新任何对象");
            return Result.failure();
        }
        classroomPlanningService.saveClassroomPlanning(classroomPlanning);
        return Result.success();
    }

    /**
     * 删除指定ID的教室计划信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "教室计划管理", summary = "删除教室计划信息")
    @DeleteMapping("")
    public Result<Void> deleteClassroomPlanningById(@RequestParam Integer id) {
        classroomPlanningService.deleteClassroomPlanningById(id);
        return Result.success();
    }

    /**
     * 查询指定ID的教室计划信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "教室计划管理", summary = "查询指定ID的教室计划信息")
//    @Parameters(value = {
//            @Parameter(description = "教室ID")
//    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教室计划ID")
    })
    @GetMapping("/{id}")
    public Result<ClassroomPlanning> findClassroomPlanningById(/*@Parameter(description = "教室ID")*/ @PathVariable Integer id) {
        ClassroomPlanning classroomPlanning = classroomPlanningService.findClassroomPlanningById(id);

        if (classroomPlanning == null) {
            return Result.failure();
        }

        return Result.success(classroomPlanning);
    }

    /**
     * 分页查询教室计划信息
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(tags = "教室计划管理", summary = "分页查询教室计划信息")
    @GetMapping("")
    public Result<PageVO<ClassroomPlanning>> listClassroomPlanningsPage(@RequestParam(defaultValue = "1") Integer page,
                                                        @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<ClassroomPlanning> list = classroomPlanningService.findByPage(page - 1, pageSize);
        PageVO<ClassroomPlanning> pageVO = PageVO.getPageVO(list);
        return Result.success(pageVO);
    }

    /**
     * 根据教室ID查询教室计划
     * @param classroomId
     * @return
     */
    @Operation(tags = "教室计划管理", summary = "根据教室ID查询教室计划")
    @GetMapping("query")
    public Result<List<ClassroomPlanning>> getClassroomPlanningsByClassroomId(@RequestParam Integer classroomId) {
            List<ClassroomPlanning> list = classroomPlanningService.findClassroomPlanningsByClassroomId(classroomId);
        return Result.success(list);
    }
}